package com.cardone.platform.common.dao;

import java.util.List;
import java.util.UUID;

import lombok.Getter;
import lombok.Setter;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import com.cardone.common.dto.PaginationDto;
import com.cardone.common.util.EntityUtils;
import com.cardone.context.Attributes;
import com.cardone.context.ContextHolder;
import com.cardone.persistent.builder.Model;
import com.cardone.persistent.builder.ModelArgs;
import com.cardone.persistent.builder.ModelUtils;
import com.cardone.persistent.support.JdbcTemplateSupport;
import com.cardone.persistent.support.PaginationArgs;
import com.cardone.platform.common.dto.EntityFileDto;

/**
 * 实体文件
 *
 * @author yaohaitao
 *
 */
@Getter
@Setter
public class EntityFileJdbcDao implements EntityFileDao {
	@Override
	public int deleteByIds(final EntityFileDto deleteEntityFile) {
		final Model model = ModelUtils.newModel();

		if (MapUtils.isNotEmpty(deleteEntityFile.getAttrs())) {
			model.putAll(deleteEntityFile.getAttrs());
		}

		model.put(Attributes.ids.name(), deleteEntityFile.getIds());

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).update(SqlIds.deleteByIds.id(), model);
	}

	@Override
	public <P> P findByCodeOrId(final Class<P> mappedClass, final EntityFileDto findEntityFile) {
		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(findEntityFile, Attributes.code.name());

		final Model model = ModelUtils.newModel(new ModelArgs(findEntityFile, whereAndEqProperties));

		if (MapUtils.isNotEmpty(findEntityFile.getAttrs())) {
			model.putAll(findEntityFile.getAttrs());
		}

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).find(mappedClass, SqlIds.findByCode.id(), model);
	}

	@Override
	public <P> P findById(final Class<P> mappedClass, final EntityFileDto findEntityFile) {
		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(findEntityFile);

		final Model model = ModelUtils.newModel(new ModelArgs(findEntityFile, whereAndEqProperties).setIsSimple(true));

		if (MapUtils.isNotEmpty(findEntityFile.getAttrs())) {
			model.putAll(findEntityFile.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).find(mappedClass, SqlIds.findById.id(), model);
	}

	@Override
	public <P> List<P> findListByLikeCode(final Class<P> mappedClass, final EntityFileDto findListEntityFile) {
		final Model model = ModelUtils.newModel(new ModelArgs(findListEntityFile).setIsSimple(true));

		if (MapUtils.isNotEmpty(findListEntityFile.getAttrs())) {
			model.putAll(findListEntityFile.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).findList(mappedClass, SqlIds.findByLikeCode.id(), model);
	}

	@Override
	public int insertByCode(final EntityFileDto insertEntityFile) {
		if (StringUtils.isBlank(insertEntityFile.getId())) {
			insertEntityFile.setId(UUID.randomUUID().toString());
		}

		final Model model = ModelUtils.newModel(new ModelArgs(Model.Keys.insert.stringValue(), insertEntityFile));

		if (MapUtils.isNotEmpty(insertEntityFile.getAttrs())) {
			model.putAll(insertEntityFile.getAttrs());
		}

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).update(SqlIds.insertByCode.id(), model);
	}

	@Override
	public <P> PaginationDto<P> paginationByLikeCode(final Class<P> mappedClass, final EntityFileDto paginationEntityFile) {
		final Model model = ModelUtils.newModel(new ModelArgs(paginationEntityFile).setIsSimple(true));

		if (MapUtils.isNotEmpty(paginationEntityFile.getAttrs())) {
			model.putAll(paginationEntityFile.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).pagination(mappedClass, new PaginationArgs(SqlIds.readByLikeCode.id(), SqlIds.findByLikeCode.id(), paginationEntityFile), model);
	}

	@Override
	public Integer readByCodeNqIdForCount(final EntityFileDto readEntityFile) {
		final String[] whereAndEqProperties = { Attributes.code.name() };

		final Model model = ModelUtils.newModel(new ModelArgs(readEntityFile, whereAndEqProperties));

		if (MapUtils.isNotEmpty(readEntityFile.getAttrs())) {
			model.putAll(readEntityFile.getAttrs());
		}

		final String[] whereAndNqProperties = EntityUtils.getWhereProperties(readEntityFile);

		if (ArrayUtils.isNotEmpty(whereAndNqProperties)) {
			ModelUtils.put(model, new ModelArgs(Model.Keys.whereAndNq.stringValue(), readEntityFile, whereAndNqProperties));
		}

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).read(Integer.class, SqlIds.readByCode.id(), model);
	}

	@Override
	public <P> P saveByIdOrCode(final Class<P> mappedClass, final EntityFileDto saveEntityFile) {
		this.insertByCode(saveEntityFile);

		return this.findByCodeOrId(mappedClass, saveEntityFile);
	}

	@Override
	public int updateByCode(final EntityFileDto updateEntityFile) {
		final EntityFileDto oldEntityFile = this.findByCodeOrId(EntityFileDto.class, updateEntityFile);

		return this.updateByCode(updateEntityFile, oldEntityFile);
	}

	private <P> int updateByCode(final EntityFileDto updateEntityFile, final P oldEntityFile) {
		if (oldEntityFile == null) {
			return 0;
		}

		final String[] useProperties = EntityUtils.addNotNullTimeSegmentProperties(new String[] { Attributes.parentId.name(), Attributes.typeId.name(), Attributes.code.name(), Attributes.name.name(), Attributes.value.name(), Attributes.remark.name() }, updateEntityFile);

		final String[] updateProperties = com.cardone.common.util.BeanUtils.diffProperties(updateEntityFile, oldEntityFile, useProperties);

		if (ArrayUtils.isEmpty(updateProperties)) {
			return 0;
		}

		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(oldEntityFile, Attributes.id.name());

		com.cardone.common.util.BeanUtils.copyProperties(oldEntityFile, updateEntityFile, whereAndEqProperties);

		final Model model = ModelUtils.newModel(new ModelArgs(Model.Keys.update.stringValue(), updateEntityFile, updateProperties), new ModelArgs(updateEntityFile, whereAndEqProperties));

		if (MapUtils.isNotEmpty(updateEntityFile.getAttrs())) {
			model.putAll(updateEntityFile.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).update(SqlIds.updateByCode.id(), model);
	}
}